<template>
  <div class="content-main">
    <a-space direction="vertical" :style="{ width: '100%' }" :size="[0, 48]">
      <a-layout>
        <a-layout-sider :style="siderStyle">
          <a-menu
            v-model:selectedKeys="selectedKeys"
            theme="dark"
            mode="inline"
          >
            <a-menu-item key="1">
              <user-outlined />
              <span class="nav-text">分类管理</span>
            </a-menu-item>
            <a-menu-item key="2">
              <video-camera-outlined />
              <span class="nav-text">标签管理</span>
            </a-menu-item>
            <a-menu-item key="3">
              <upload-outlined />
              <span class="nav-text">用户管理</span>
            </a-menu-item>
          </a-menu>
        </a-layout-sider>

        <a-layout>
          <a-layout-header :style="headerStyle">
            <a-layout-header class="header">
              <div class="logo" />
              <a-menu
                v-model:selectedKeys="selectedKeys1"
                theme="dark"
                mode="horizontal"
                :style="{ lineHeight: '64px' }"
              >
                <a-menu-item key="1">运营中台</a-menu-item>
                <a-menu-item key="2">市民通</a-menu-item>
                <a-menu-item key="3">智慧教育</a-menu-item>
              </a-menu>
            </a-layout-header>
          </a-layout-header>

          <a-layout-content :style="contentStyle">
            <a-tree
              class="draggable-tree"
              draggable
              block-node
              :tree-data="gData"
              @dragenter="onDragEnter"
              @drop="onDrop"
            />
          </a-layout-content>

          <a-layout-footer :style="footerStyle"> </a-layout-footer>
        </a-layout>
      </a-layout>
    </a-space>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import type { CSSProperties } from "vue";
const siderStyle: CSSProperties = {
  textAlign: "center",
  lineHeight: "100vh",
  color: "#fff",
  backgroundColor: "#3ba0e9",
};

const headerStyle: CSSProperties = {
  textAlign: "center",
  color: "#fff",
  height: 64,
  paddingInline: 50,
  lineHeight: "64px",
  backgroundColor: "#7dbcea",
};

const contentStyle: CSSProperties = {
  textAlign: "center",
  minHeight: 120,
  lineHeight: "120px",
  color: "#fff",
  backgroundColor: "#108ee9",
};

const footerStyle: CSSProperties = {
  textAlign: "center",
  color: "#fff",
  backgroundColor: "#7dbcea",
};

import type {
  AntTreeNodeDragEnterEvent,
  AntTreeNodeDropEvent,
  TreeProps,
} from "ant-design-vue/es/tree";

const x = 3;
const y = 2;
const z = 1;
const genData = [];

const generateData = (
  _level: number,
  _preKey?: string,
  _tns?: TreeProps["treeData"]
) => {
  const preKey = _preKey || "0";
  const tns = _tns || genData;

  const children = [];
  for (let i = 0; i < x; i++) {
    const key = `${preKey}-${i}`;
    tns.push({ title: key, key });
    if (i < y) {
      children.push(key);
    }
  }
  if (_level < 0) {
    return tns;
  }
  const level = _level - 1;
  children.forEach((key, index) => {
    tns[index].children = [];
    return generateData(level, key, tns[index].children);
  });
};
generateData(z);
type TreeDataItem = TreeProps["treeData"][number];
const gData = ref<TreeProps["treeData"]>(genData);
const onDragEnter = (info: AntTreeNodeDragEnterEvent) => {
  console.log(info);
  // expandedKeys 需要展开时
  // expandedKeys.value = info.expandedKeys;
};

const onDrop = (info: AntTreeNodeDropEvent) => {
  console.log(info);
  const dropKey = info.node.key;
  const dragKey = info.dragNode.key;
  const dropPos = info.node.pos.split("-");
  const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]);
  const loop = (
    data: TreeProps["treeData"],
    key: string | number,
    callback: any
  ) => {
    data.forEach((item, index) => {
      if (item.key === key) {
        return callback(item, index, data);
      }
      if (item.children) {
        return loop(item.children, key, callback);
      }
    });
  };
  const data = [...gData.value];

  // Find dragObject
  let dragObj: TreeDataItem;
  loop(
    data,
    dragKey,
    (item: TreeDataItem, index: number, arr: TreeProps["treeData"]) => {
      arr.splice(index, 1);
      dragObj = item;
    }
  );
  if (!info.dropToGap) {
    // Drop on the content
    loop(data, dropKey, (item: TreeDataItem) => {
      item.children = item.children || [];
      /// where to insert 示例添加到头部，可以是随意位置
      item.children.unshift(dragObj);
    });
  } else if (
    (info.node.children || []).length > 0 && // Has children
    info.node.expanded && // Is expanded
    dropPosition === 1 // On the bottom gap
  ) {
    loop(data, dropKey, (item: TreeDataItem) => {
      item.children = item.children || [];
      // where to insert 示例添加到头部，可以是随意位置
      item.children.unshift(dragObj);
    });
  } else {
    let ar: TreeProps["treeData"] = [];
    let i = 0;
    loop(
      data,
      dropKey,
      (_item: TreeDataItem, index: number, arr: TreeProps["treeData"]) => {
        ar = arr;
        i = index;
      }
    );
    if (dropPosition === -1) {
      ar.splice(i, 0, dragObj);
    } else {
      ar.splice(i + 1, 0, dragObj);
    }
  }
  gData.value = data;
};

const selectedKeys = ref<string[]>(["4"]);
const selectedKeys1 = ref<string[]>(["2"]);
</script>

<style lang="less">
.content-main {
  background-color: #f5f5f5;
  background-size: cover;
  width: 100vw;
  height: 100vh;
}
</style>
